import javax.swing.*;
import java.awt.*;
import java.awt.event.ActionEvent;
import java.awt.event.ActionListener;
import java.util.HashMap;

public class Tra extends JFrame implements ActionListener {
    JPanel p1, p2;
    private JTextArea show;
    private String[] text = {
            "查询单词", "添加词典", "删除词典", "修改词典", "收藏词典"
    };
    private JButton[] Button = new JButton[text.length];
    private HashMap<String, String> dictionary = new HashMap<String, String>();

    public Tra() {
        p1 = new JPanel();
        p2 = new JPanel();
        show = new JTextArea();
        p1.setSize(1000, 40);
        p2.setSize(1000, 230);
    }

    public void init() {//初始化
        this.setTitle("中英互译查询软件");
        this.setBounds(200, 200, 1000, 270);
        this.setLocation(100, 300);
        this.setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE);
        this.setLayout(new BorderLayout());
        this.add(p1, BorderLayout.NORTH);
        this.add(p2, BorderLayout.CENTER);
        this.setVisible(true);

        JLabel label = new JLabel("输入你要查询的单词");
        p1.add(label);
        label.setPreferredSize(new Dimension(180, 40));
        show.setPreferredSize(new Dimension(200, 30));
        p1.add(show);

        // 添加单词到词典中
//        dictionary.put("hello", "你好");
//        dictionary.put("world", "世界");
//        dictionary.put("apple", "苹果");
//        dictionary.put("你好", "hello");
//        dictionary.put("世界", "world");


        for (int i = 0; i < text.length; i++) {
            Button[i] = new JButton(text[i] + " ");
            p1.add(Button[i]);
            JTextArea show1 = new JTextArea();
            show1.setPreferredSize(new Dimension(600, 200));
            p2.add(show1);

            // 查询单词按钮的ActionListener
            if (i == 0) {
                Button[i].addActionListener(new ActionListener() {
                    public void actionPerformed(ActionEvent e) {
                        String word = show.getText();
                        String translation = dictionary.get(word);
                        if (translation != null) {
                            show1.setText(translation);
                        } else {
                            show1.setText("未找到该单词的翻译");
                        }
                    }
                });
            }
            // 添加词典按钮的ActionListener
            else if (i == 1) {
                Button[i].addActionListener(new ActionListener() {
                    @Override
                    public void actionPerformed(ActionEvent e) {
                        String word = show.getText();
                        String translation = dictionary.get(word);

                        if (translation != null) {
                            show1.setText("该单词已存在于词典中");
                        } else {
                            dictionary.put(word, "");

                            //show1.setText("成功添加单词到词典中");
                            // dictionary.put("", word);
                        }
                        show1.setText("成功添加单词到词典中");
                    }
                });
            }
            // 删除词典按钮的ActionListener
            else if (i == 2) {
                Button[i].addActionListener(new ActionListener() {
                    public void actionPerformed(ActionEvent e) {
                        String word = show.getText();
                        String translation = dictionary.get(word);
                        if (translation != null) {
                            dictionary.remove(word);
                            show1.setText("成功从词典中删除单词");
                        } else {
                            show1.setText("词典中不存在该单词");
                        }
                    }
                });
            }
            // 修改词典按钮的ActionListener
            else if (i == 3) {
                Button[i].addActionListener(new ActionListener() {
                    public void actionPerformed(ActionEvent e) {
                        String word = show.getText();
                        String translation = dictionary.get(word);
                        if (translation != null) {
                            dictionary.put(word, "");
                            show1.setText("成功修改词典中的单词");
                        } else {
                            show1.setText("词典中不存在该单词");
                        }
                    }
                });
            }
            // 收藏词典按钮的ActionListener
            else if (i == 4) {
                Button[i].addActionListener(new ActionListener() {
                    public void actionPerformed(ActionEvent e) {
                        String word = show.getText();
                        String translation = dictionary.get(word);
                        if (translation != null) {
                            show1.setText("成功收藏该单词");
                        } else {
                            show1.setText("词典中不存在该单词");
                        }
                    }
                });
            }
        }
    }

    public static void main(String[] args) {
        new Tra().init();
    }

    @Override
    public void actionPerformed(ActionEvent e) {

    }
}
